﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Tutorial_Day1
{
    public partial class frmMainTree : Form
    {
        #region Properties
        private int _iStudentId = 0;
        private string _sStudentName = string.Empty;
        private DateTime _dtDOB = new DateTime();

        public int StudentId
        {
            get { return _iStudentId; }
            set { _iStudentId = value; }
        }
        public string StudentName
        {
            get { return _sStudentName; }
            set { _sStudentName = value; }
        }

        #endregion

        #region Constructor
        public frmMainTree()
        {
            InitializeComponent();
        }

        #endregion

        private void LoadTree()
        {
            try
            {
                //Clear the Tree, before loading data
                tvStudentList.Nodes.Clear();

                //This is the PARENT Node
                tvStudentList.Nodes.Add("Thirukurral", "Thirukurral");


                //Load 133 Chapters of Thirukurral
                //Each Chapater has 10 Kurals. So 133 X 10 = 1330


                //This Loop adds Each Chapter with Kural 
                for (int i = 1; i <= 133; i++)
                {
                    //Loading Chapter

                    //NOTE :Nodes.Add methods takes two parameter. First one Name, Second one Text
                    tvStudentList.Nodes["Thirukurral"].Nodes.Add("Chapter " + i.ToString(), "Chapter " + i.ToString());

                    //Loading 10 Kurals for this Chapter
                    for (int j = 1; j <= 10; j++)
                    {
                        //TreeNodeCollection tc = tvStudentList.Nodes["Thirukurral"].Nodes;
                        tvStudentList.Nodes["Thirukurral"].Nodes["Chapter " + i.ToString()].Nodes.Add("Kural " + j.ToString(), "Kural " + j.ToString());
                    }
                }
                tvStudentList.CollapseAll ();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Thirukural", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            LoadTree();
        }

      
        private void btnCollapse_Click(object sender, EventArgs e)
        {
            if (tvStudentList.Nodes != null)
            {
                tvStudentList.CollapseAll();
            }
        }

        private void btnExpand_Click(object sender, EventArgs e)
        {
            if (tvStudentList.Nodes != null)
            {
                tvStudentList.ExpandAll();
            }
        }

        private void tvStudentList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //SelectedNode.Level =1 -> Thirukurral (Parent Node)
            //SelectedNode.Level =2 -> Chapters (Second Level Node)
            //SelectedNode.Level =2 -> Kural (Third Level Nodes)
            if (tvStudentList.SelectedNode.Level == 2)
            {
                int iChapterNo = tvStudentList.SelectedNode.Parent.Index + 1;
                int iKuralNo = tvStudentList.SelectedNode.Index + 1;

                lblChapter.Text = "Chapter Number is :" + iChapterNo.ToString();
                lblKuralNo.Text = "Kural Number is :" + iKuralNo.ToString();

                //Actual Kural No is  = ( iChapterNo X 100 ) + iKuralNo
                // For Example if Chapter is 150 and Kural No is 5,  then (150 x 100) +5 = 1505
                //Based on this, fetch Data from Database, and show on the other Labels too.
                 int iKuralListNo =0;
                 if (iChapterNo != 1)
                 {
                     iKuralListNo = ((iChapterNo -1 )* 10) + iKuralNo;
                 }
                 else
                 {
                     iKuralListNo = iKuralNo;
                 }

                DataTable dtKural = GetKuralByNumber(iKuralListNo);
                if (dtKural != null && dtKural.Rows.Count > 0)
                {
                    //Load the data to Labels
                    lblKuralDetail.Text = dtKural.Rows[0]["KuralDescription"].ToString();
                }
              //  lblKuralDetail.Text = "Based on this, fetch Data from Database, and show on this label too.";
                
            }
            else
            {
                ClearText();
            }
        }
        private void ClearText()
        {
            lblChapter.Text = lblKuralDetail.Text  = lblKuralNo.Text  = string.Empty;
        }
        private DataTable GetKuralByNumber(int iKuralNo)
        {
            DataTable dtKural = new DataTable();

            string sQuery = "SELECT KuralId,KuralDescription,Meaning FROM ThiruKuralListTable WHERE KuralId=" + iKuralNo.ToString ();
            DBManager objDBManager = new DBManager();
            dtKural = objDBManager.FetchTable(sQuery);

            return dtKural;
        }
    }
}
